package com.alumni_circle.controller.home.addition;

import com.alumni_circle.core.config.SysLog;
import com.alumni_circle.core.entity.BaseId;
import com.alumni_circle.core.entity.PageBase;
import com.alumni_circle.core.entity.ResponseListModel;
import com.alumni_circle.core.entity.ResponseModel;
import com.alumni_circle.core.utils.ModelUtils;
import com.alumni_circle.entity.home.Announcement;
import com.alumni_circle.entity.home.LostFound;
import com.alumni_circle.entity.home.VO.AnnouncementInsertVO;
import com.alumni_circle.entity.home.VO.AnnouncementSearchVO;
import com.alumni_circle.entity.home.VO.AnnouncementUpdateVO;
import com.alumni_circle.entity.home.VO.AnnouncementViewVO;
import com.alumni_circle.service.home.impl.AnnouncementServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.security.core.parameters.P;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

@RequestMapping("/announcement")
@Controller
public class AnnouncementController {

    @Resource
    AnnouncementServiceImpl announcementService;

    @ResponseBody
    @RequestMapping(value = "/getAllAnnouncement", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "查询所有公告", notes = "查询所有公告", httpMethod = "POST", response = ResponseModel.class)
    @SysLog(type = "查", accessInfo = "查询所有公告", url = "/announcement/getAllAnnouncement")
    public ResponseListModel getAllAnnouncement(@ApiParam(required = true, name = "pageBase")
                                             @RequestBody PageBase pageBase) {
        PageHelper.startPage(pageBase.getPageNum(), pageBase.getPageSize());
        List<Announcement> list = announcementService.list();
        PageInfo<Announcement> pageInfo = new PageInfo<>(list);
        System.out.println(String.valueOf(pageInfo.getTotal()));
        return new ResponseListModel(ModelUtils.copyListProperties(list, AnnouncementViewVO.class), String.valueOf(pageInfo.getTotal()));
    }

    @ResponseBody
    @RequestMapping(value = "/searchAnnouncement", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "搜索所有公告", notes = "搜索所有公告", httpMethod = "POST", response = ResponseModel.class)
    @SysLog(type = "查", accessInfo = "搜索所有公告", url = "/announcement/searchAnnouncement")
    public ResponseListModel searchAnnouncement(@ApiParam(required = true, name = "pageBase")
                                             @RequestBody AnnouncementSearchVO announcementSearchVO) {
        PageHelper.startPage(announcementSearchVO.getPageNum(), announcementSearchVO.getPageSize());
        List<Announcement> list = announcementService.list(new QueryWrapper<Announcement>()
                                                            .like("update_time",announcementSearchVO.getUpdateTime())
                                                            .like("update_name",announcementSearchVO.getUpdateName())
                                                            .like("title",announcementSearchVO.getTitle())
                                                            .like("comment",announcementSearchVO.getComment()));
        PageInfo<Announcement> pageInfo = new PageInfo<>(list);
        return new ResponseListModel(ModelUtils.copyListProperties(list, AnnouncementViewVO.class), String.valueOf(pageInfo.getTotal()));
    }

    @ResponseBody
    @RequestMapping(value = "/updateAnnouncement", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "更新公告", notes = "更新公告", httpMethod = "POST", response = ResponseModel.class)
    @SysLog(type = "改", accessInfo = "更新公告", url = "/announcement/updateAnnouncement")
    public ResponseListModel getAllAnnouncement(@ApiParam(required = true, name = "pageBase")
                                                @RequestBody AnnouncementUpdateVO announcementUpdateVO) {
        announcementService.updateById(ModelUtils.transform(announcementUpdateVO,Announcement.class));
        return new ResponseListModel();
    }

    @ResponseBody
    @RequestMapping(value = "/delAnnouncement", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "删除公告", notes = "删除公告", httpMethod = "POST", response = ResponseModel.class)
    @SysLog(type = "删", accessInfo = "删除公告", url = "/announcement/delAnnouncement")
    public ResponseListModel delAnnouncement(@ApiParam(required = true, name = "pageBase")
                                                @RequestBody BaseId baseId) {
        announcementService.removeById(baseId.getId());
        return new ResponseListModel();
    }

    @ResponseBody
    @RequestMapping(value = "/addAnnouncement", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "新增公告", notes = "新增公告", httpMethod = "POST", response = ResponseModel.class)
    @SysLog(type = "增", accessInfo = "新增公告", url = "/announcement/addAnnouncement")
    public ResponseListModel addAnnouncement(@ApiParam(required = true, name = "pageBase")
                                             @RequestBody AnnouncementInsertVO announcementInsertVO) {
        announcementService.save(ModelUtils.transform(announcementInsertVO,Announcement.class));
        return new ResponseListModel();
    }
}
